const fs = require('fs');
const path = require('path');


fileName = 'input.txt';
filePath = path.resolve(__dirname, fileName);

let myEvent = {
    event:{},
    on: function(eventName, eventFn){
        if(this.event[eventName]){
            this.event[eventName].push(eventFn)
        }else{
            this.event[eventName] = [eventFn]
        }
    },

    emit: function(eventName, eventMsg){
        if(this.event[eventName]){
            this.event[eventName].forEach(itemFn=>{
                // itemFn.apply(this, eventMsg)
                itemFn(eventMsg)
            })
        }
    }
}


fs.readFile(filePath, {flag:"r", encoding:"utf-8"}, (err,data)=>{
    if(err){
        console.log(err);
    }else{
        console.log(data);
        myEvent.emit("fileSuccess", "ok")
    }
})

myEvent.on('fileSuccess', eventMsg=>{
    console.log(1)
    console.log(eventMsg)
})

myEvent.on('fileSuccess', eventMsg=>{
    console.log(2)
    console.log(eventMsg)
})

myEvent.on('fileSuccess', eventMsg=>{
    console.log(3)
    console.log(eventMsg)
})